<?php

namespace app\api\tests;

require_once 'D:\software\phpstudy_pro\WWW\local_project\easyadmin\vendor\autoload.php';//绝对路径引入自动加载文件
// ### 需要手动初始化
use app\api\traits\CommonTrait;
use think\Exception;
use function PHPUnit\Framework\isJson;

trait TestTrait
{
    /**
     * @desc get请求
     * @param $url
     * @return array|mixed
     */
    public function get($url)
    {
        try {
            $res = $this->curlGet($url);
            // 不是json数据
            $resArr = json_decode($res, true);
            if (is_null($resArr)) {
                return $res;
            }
            return $resArr;
        } catch (Exception $e) {
            return $e->getTrace();
        }
    }

    /**
     * 构造一个请求应用，测试控制器
     *
     * @param array $data 入参数组
     * @param string $method 请求方法
     * @return \think\App
     */
    public function visitorJson($controller, $action, $data, $uid = null, $method = 'POST')
    {
        $app = new \think\App();

        #$app->request->setMethod($method);
        //$app->request->setController('Xlogin');
        //$app->request->setUrl('/index/debug/testing26');
        //$app->request->setUrl('/index/oauth/login');
        //$app->request->setBaseUrl('/index/oauth/login');

//        $app->request->setUrl("api/{$controller}/{$action}}");

        //$app->request->withGet(['xid' => 99,'xx' => 'get-xx','yy' => 'get-yy',]);
        //$app->request->withGet($data);
        //$app->request->withPost(['aaa' => 111, 'bbb' => 222,'xx' => 'post-xx','yy' => 'post-yy',]);
        #$app->request->withPost($data);

//        $request = new \app\Request();
//        $request->withPost($data);
//        $request->withRoute($data); //tp6的bug啊，一行代码曲线救国

//        $response = $app->http->run($request);
        //重置请求对象
//        $app->request->setMethod($method);//非必须
//        $app->request->setAction($action);
        //$app->request->withGet($data);
        //$app->request->withPost($data);
        //$response->send();
//        $app->http->end($response);


//        $controller = "\\app\\api\\controller\\" . $controller;
//
//        $test    = new $controller($app);
//        $result  = $test->$action();
//        $content = $result->getContent();

        $app->request->setUrl("/api/{$controller}/{$action}}");
        $res = $app->request->withGet($data);
        $app->withRoute($data);
        print_r($res);
        die;
        return json_decode($content, true);
    }
    /**
     * @desc curl get方法
     * @param string $url 请求url
     * @return mixed
     */
    public function curlGet($url = '', $options = [], $header = [])
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 设置HTTP头
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    /**
     * @desc curl post方法
     * @param string $url
     * @param array $data 数据
     * @return mixed
     */
    public function curlPost($url = '', $data = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * @desc解析data里参数
     * @param string $index 索引参数
     * @return array
     */
    public function parseData($index)
    {
        $arr = include "data.php";// 引入文件
        $rest = $arr[$index];
        foreach ($rest as &$v) {
            // 解析json数据为数组
            $v['param'] = json_decode($v['param'] ,true);
            $v['ret'] = json_decode($v['ret'] ,true);
        }
        return $rest;
    }
}